package com.gylang.im.core.util;


import org.springframework.util.Base64Utils;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

/**
 * @author gylang,
 * date 2020/5/6,
 * @version 1.0
 */
public class AesUtils {
    /**
     * 加密方式
     */
    private static final String KEY_ALGORITHM = "AES";
    /**
     * 默认的加密算法
     */
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String SECRET = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtR0szfDk0RmuxJzyraw3/GEU2TToXwk2bMrzxAzBm+0xRwVgiBQCV7gvePfV02xl8SbYp9EgRObKy8vNdXLkU6kFg4+yzshpRJwSAyIa1UvJwHG6lUpHTa9Tvc9M0f7UATuqF/vLCjX96vQwRtIj9SB47J9f3YLkqcVEyw1aoGrFox65Tr1CNnInSsBpvGpzBoGtWWhKz/Q45M29GUWX+qVp3Td997z+RZZFDXEwNR+YA7vkAcxIOVcLJISzrDyJtbZ/nhlU2CDnuzWZGELvwNzHxmH9DK9U94ZuzB6ubbSvk1zF1Kj9bq72fc50g7IeQFjAiVF47Vg220LtZK0/6wIDAQAB";


    public static String encrypt(String content, String key) {

        try {
            //创建密码器
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            //获取加密数据byte
            byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);

            //初始化加密密码器
            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));

            byte[] bytes = cipher.doFinal(contentBytes);

            return new String(Base64Utils.encode(bytes), StandardCharsets.UTF_8);

        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 解密数据
     *
     * @param content 解密类容
     * @return 解密后数据
     */
    public static String decrypt(String content, String key) {

        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);

            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));

            byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);

            byte[] bytes = cipher.doFinal(Base64Utils.decode(contentBytes));

            return new String(bytes, StandardCharsets.UTF_8);

        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
            e.printStackTrace();
        }
        return null;

    }


    /**
     * 秘钥对象生成器
     *
     * @return 秘钥对象
     */
    private static SecretKeySpec getSecretKey(String key) {

        try {

            KeyGenerator instance = KeyGenerator.getInstance(KEY_ALGORITHM);
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(key.getBytes());
            instance.init(128, secureRandom);
            SecretKey secretKey = instance.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;

    }


}
